var limsExportTemplate = Gikam.getPageObject({

    getEditListColumns: function () {
        return [{
            checkbox: true
        }, {
            index: true
        }, {
            field: 'templateCode',
            validators: ['notEmpty'],
            title: 'T_LIMS_EXPORT_TEMPLATE.TEMPLATECODE',
            width: 220
        }, {
            field: 'templateName',
            title: 'T_LIMS_EXPORT_TEMPLATE.TEMPLATENAME',
            editor: true,
            validators: ['notEmpty'],
            width: 170
        }, {
            field: 'fileName',
            title: 'T_LIMS_EXPORT_TEMPLATE.FILENAME',
            type: 'link',
            width: 170
        }, {
            field: 'remark',
            title: 'T_LIMS_EXPORT_TEMPLATE.REMARK',
            width: 150,
            editor: true
        }, {
            field: 'option',
            title: 'T_LIMS_REPORT_TEMPLATE.OPTION',
            contentAlign: 'center',
            width: 180,
            formatter: function (index, value, row) {
                var fileFlag = Gikam.isNotEmpty(row.templateFileId);
                return Gikam.printf('<a href="javascript:;" onclick="limsExportTemplate.uploadTemplateFile(\'{id}\', \'{index}\', \'{dbTable}\')">{name}</a>&nbsp;&nbsp;<a href="javascript:;" onclick="limsExportTemplate.downloadTemplate(\'{templateCode}\',\'{fileId}\')">{download}</a>', {
                    id: row.id,
                    index: index,
                    dbTable: 'T_LIMS_EXPORT_TEMPLATE',
                    templateCode: row.templateCode,
                    fileId: row.templateFileId,
                    name: fileFlag ? Gikam.propI18N('GIKAM.AT.UPDATE') : Gikam.propI18N('CORE.MODULE.EQUIPT.FILEACQCONFIGS.BUTTON.UPLOAD'),
                    download: Gikam.propI18N('CORE.MODULE.SYS.MONITOR.ERRORLOGS.BUTTON.DOWNLOAD'),
                });
            }
        }];
    },

    getBaseInfoFormFields: function () {
        return [{
            field: 'templateCode',
            title: 'T_LIMS_EXPORT_TEMPLATE.TEMPLATECODE',
            validators: [{type: 'unique', rule: '/secure/baseline/basedata/export-templates/action/validate-unique'}],
            preInsert: true
        }, {
            field: 'templateName',
            title: 'T_LIMS_EXPORT_TEMPLATE.TEMPLATENAME',
            preInsert: true
        }, {
            field: 'fileName',
            title: 'T_LIMS_EXPORT_TEMPLATE.FILENAME',
            validators: ['strLength[0,33]']
        }, {
            field: 'remark',
            title: 'T_LIMS_EXPORT_TEMPLATE.REMARK',
            validators: ['strLength[0,66]']
        },];
    },

    uploadTemplateFile: function (id, index, dbTable) {
        var _this = this;
        Gikam.create('simpleUploader', {
            dbTable: dbTable,
            bizId: id,
            fill: true,
            multiple: false,
            accept: ['xls', 'xlsx', 'docx'],
            onBeforeFileUpload: function (file) {
                var flag = false;
                if (file.name.indexOf(".xls") > -1) {
                    flag = true;
                }
                if (file.name.indexOf(".xlsx") > -1) {
                    flag = true;
                }
                if (file.name.indexOf(".docx") > -1) {
                    flag = true;
                }
                return flag;
            },
            onUploadSuccess: function (data) {
                Gikam.getComp("lims-export-template-edit-list-grid").setData({
                    id: id,
                    index: index,
                    templateFileId: data[0].id,
                    fileName: data[0].name
                }, true);
            }
        });
    },

    downloadTemplate: function (templateId, fileId) {
        let url = Gikam.printf(limsExportTemplate.baseUrl + '/download-template/{templateId}/{fileId}', {
            templateId: templateId,
            fileId: fileId
        })
        Gikam.postSync(url, null).done(file => {
            if (file) {
                Gikam.download(file.downloadUrl)
            }
        })
    },

    baseUrl: IFM_CONTEXT + '/secure/baseline/basedata/export-templates',


    getChooseListColumns: function () {
        return [{
            index: true
        }, {
            field: 'templateCode',
            title: 'T_LIMS_EXPORT_TEMPLATE.TEMPLATECODE',
            width: 220
        }, {
            field: 'templateName',
            title: 'T_LIMS_EXPORT_TEMPLATE.TEMPLATENAME',
            width: 170
        }, {
            field: 'fileName',
            title: 'T_LIMS_EXPORT_TEMPLATE.FILENAME',
            width: 170
        }, {
            field: 'remark',
            title: 'T_LIMS_EXPORT_TEMPLATE.REMARK',
            width: 150,
        }];
    },
});
